﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using Entity;
using Common;

namespace DataAccess
{
    public class StudentDA
    {
        public static List<Student> generateStudent(Classs cl)
        {
            List<Student> Students = null;
            using (SqlConnection conn = ConnectData.getConnection())
            {
                try
                {
                    conn.Open();
                    SqlCommand cm = new SqlCommand("View_All_Student_Branch_Class", conn);
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.Parameters.Add("@ClassID", SqlDbType.Int);
                    cm.Parameters["@ClassID"].Value = cl.ClassID;
                    SqlDataAdapter da = new SqlDataAdapter(cm);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    //Students = new List<Student>();
                    //foreach(DataRow row in ds.Tables[0].Rows){
                    //    Students.Add(new Student { StudentID = row["StudentID"].ToString(), Name = row["Name"].ToString(), CodeTrainningName = row["CodeTrainingName"].ToString(), CurrentClassID = Int32.Parse(row["CurrentClassID"].ToString()), Gender = row["Gender"].ToString() == "True" ? GenderType.Nam : GenderType.Nữ, BirthDay = (DateTime)row["BirthDay"], Address = row["Addresss"].ToString(), Phone = row["Phone"].ToString(), Email = row["Email"].ToString(), StudentStatus = Int32.Parse(row["StudentStatus"].ToString()), Descriptions = row["Descriptions"].ToString() });
                    //}
                    Students = LibConvert.ConvertTo<Student>(ds.Tables[0]);

                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("generateStudent: " + ex.Message);
                }
            }
            return Students;
        }
    }
}
